

package org.smallbun.fast.manage.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.smallbun.fast.common.PageFactory;
import org.smallbun.fast.manage.activity.entity.ActivityEntity;
import org.smallbun.fast.manage.activity.service.ActivityService;
import org.smallbun.framework.annotation.DemoEnvironment;
import org.smallbun.framework.base.BaseController;
import org.smallbun.framework.result.AjaxResult;
import org.smallbun.framework.result.PageableResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.smallbun.framework.constant.ErrorMsgConstant.ID_NOT_BLANK_MSG;
import static org.smallbun.framework.toolkit.AutoMapperUtil.mappingList;


/**
 * @author lovec
 */
@Validated
@RestController
@RequestMapping(value = "/activity")
public class ActivityController extends BaseController {
	/**
	 * HTML页面路径前缀
	 */
	private static final String HTML_PREFIX = "modules/manage/activity/";
	/**
	 * 模块名称
	 */
	private static final String MODEL = "活动模块";

	public ActivityController(ActivityService activityService) {
		this.activityService = activityService;
	}


	/**
	 * 返回list页面
	 * @return {@link ModelAndView}
	 */
	@GetMapping(value = {"", "/"})
	@PreAuthorize("hasAuthority('manage:activity:index')")
	public ModelAndView list() {
		return new ModelAndView(HTML_PREFIX + "activity_list.html");
	}

	/**
	 * form表单
	 * @return 地址
	 */
	@GetMapping(value = "/form")
	@PreAuthorize("hasAuthority('manage:activity:add') or hasAuthority('manage:activity:edit')")
	public ModelAndView form(ActivityEntity vo, Model model) {
		model.addAttribute("role", vo);
		return new ModelAndView(HTML_PREFIX + "activity_form.html");
	}

	/**
	 * 保存或更新
	 * @param vo 实体对象
	 * @return AjaxResult
	 */
	@DemoEnvironment
	@RequestMapping(value = "/saveOrUpdate")
	@PreAuthorize("hasAuthority('manage:activity:add') or hasAuthority('manage:role:edit')")
	public AjaxResult saveOrUpdate(@Valid ActivityEntity vo) {
		return AjaxResult.builder().result(null).build();
	}

	/**
	 * 删除单条记录
	 * @param id 主键ID
	 * @return AjaxResult
	 */
	@DemoEnvironment
	@PostMapping(value = "/removeById")
	@PreAuthorize("hasAuthority('manage:activity:del')")
	public AjaxResult removeById(
			@NotBlank(message = ID_NOT_BLANK_MSG) @RequestParam(value = "id", required = false) String id) {
		return AjaxResult.builder().result(activityService.removeById(id)).build();
	}

	/**
	 * 删除多条记录
	 * @param ids 主键ID集合
	 * @return AjaxResult
	 */
	@DemoEnvironment
	@PostMapping(value = "/removeByIds")
	@PreAuthorize("hasAuthority('manage:activity:del')")
	public AjaxResult saveOrUpdate(
			@NotBlank(message = ID_NOT_BLANK_MSG) @RequestParam(value = "ids", required = false) List<String> ids) {
		return AjaxResult.builder().result(activityService.removeByIds(ids)).build();
	}

	/**
	 * 分页查询
	 * @return PageableResult
	 */
	@PostMapping(value = "/page")
	public PageableResult page(Page<ActivityEntity> page, ActivityEntity vo) {
		return PageableResult.builder().page(pageVOFilling(
				activityService.page(new PageFactory<ActivityEntity>().defaultPage(page), new QueryWrapper<>(vo)),
				ActivityEntity.class)).build();
	}

	/**
	 * 查询全部记录
	 * @return SysDictTypeEntity
	 */
	@PostMapping(value = "/list")
	public AjaxResult list(ActivityEntity vo) {
		return AjaxResult.builder()
				.result(mappingList(activityService.list(new QueryWrapper<>(vo)).stream().filter(i->i.getIsPublish()==1).collect(Collectors.toList()), new ArrayList<>(), ActivityEntity.class))
				.build();
	}

	@GetMapping(value = "/get/{id}")
	public AjaxResult get(@PathVariable("id") long id){
		return AjaxResult.builder().result(activityService.getById(id)).build();
	}



	/**
	 * 注入用户角色service接口
	 */
	private final ActivityService activityService;
}
